<template>
  <el-select
    :value="selectValue"
    :size="size"
    clearable
    :multiple="multiple"
    :disabled="readonly"
    @input="handleInput"
  >
    <el-option
      v-for="item in dictionaryItemList"
      :key="item.code"
      :value="item.code"
      :label="item.label"
    />
  </el-select>
</template>

<script>

export default {
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: [String, Array],
      required: false,
      default: ''
    },
    code: {
      type: String,
      default: ''
    },
    readonly: {
      type: Boolean,
      required: false,
      default: false
    },
    size: {
      type: String,
      default: ''
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectValue: '',
      dictionaryItemList: []

    }
  },
  watch: {
    value: {
      immediate: true,
      handler: 'handleValue'
    }
  },
  mounted() {
    this.dictionaryItemList = []
    this.$api.system.dictionaryType.getItem(this.code).then(res => {
      this.dictionaryItemList = res.data
    })
  },
  methods: {
    handleInput: function (selectedValue) {
      this.$emit('change', selectedValue)
    },
    handleValue: function () {
      this.selectValue = this.value
    },
    // 根据value获取对应的label值
    getLabel(value) {
      const item = this.dictionaryItemList.find(x => x.code === value)
      if (item) {
        return item.label
      }
      return ''
    }
  }
}
</script>
